package com.hbsx.why.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbsx.why.R;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.utils.ToastUtils;
import com.hbsx.why.utils.refresh.PullScrollView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.hbsx.why.utils.refresh.listener.ScrollViewListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liub
 * time 2017/8/29
 */

public class ActivityDetailActivity extends BaseActivity {
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.left_activity_btnv)
    Button leftActivityBtnv;
    @BindView(R.id.right_activity_btnv)
    Button rightActivityBtnv;
    @BindView(R.id.tab_linearv)
    LinearLayout tabLinearv;
    @BindView(R.id.left_activity_btn)
    Button leftActivityBtn;
    @BindView(R.id.right_activity_btn)
    Button rightActivityBtn;
    @BindView(R.id.tab_linear)
    LinearLayout tabLinear;
    @BindView(R.id.my_PullScrollView)
    PullScrollView myPullScrollView;
    @BindView(R.id.my_PullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    private int[] location = new int[2];
    private int[] location2 = new int[2];
    private int scrolly = 0;
    private boolean scrollflag = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_activity_detail;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText("活动详情");
        myPullScrollView.setPullDownEnable(true);
        myPullScrollView.setPullUpEnable(false);
        myPullScrollView.setScrollViewListener(new ScrollViewListener() {
            @Override
            public void onScrollChanged(PullScrollView scrollView, int x, int y, int oldx, int oldy) {
                tabLinear.getLocationOnScreen(location);
                tabLinearv.getLocationOnScreen(location2);
                if (!scrollflag) {
                    scrollflag = true;
                    scrolly = location2[1] - tabLinear.getHeight();
                }
                if (location[1] > location2[1]) {
                    tabLinear.setVisibility(View.VISIBLE);
                } else {
                    tabLinear.setVisibility(View.GONE);
                }
            }
        });
        setTabSelected(leftActivityBtn, tabLinear);
        setTabSelected(leftActivityBtnv, tabLinearv);
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        myPullToRefreshLayout.refreshFinish(true);
                        showToast("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

            }
        });
    }


    @OnClick({R.id.left_activity_btnv, R.id.right_activity_btnv, R.id.left_activity_btn, R.id.right_activity_btn, R.id.map_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_activity_btnv:
                setTabSelected(leftActivityBtnv, tabLinearv);
                setTabSelected(leftActivityBtn, tabLinear);
                myPullScrollView.scrollTo(0, scrolly);
                break;
            case R.id.right_activity_btnv:
                setTabSelected(rightActivityBtnv, tabLinearv);
                setTabSelected(rightActivityBtn, tabLinear);
                myPullScrollView.scrollTo(0, myPullScrollView.getMeasuredHeight());
                break;
            case R.id.left_activity_btn:
                setTabSelected(leftActivityBtnv, tabLinearv);
                setTabSelected(leftActivityBtn, tabLinear);
                myPullScrollView.scrollTo(0, scrolly);
                break;
            case R.id.right_activity_btn:
                setTabSelected(rightActivityBtnv, tabLinearv);
                setTabSelected(rightActivityBtn, tabLinear);
                myPullScrollView.scrollTo(0, myPullScrollView.getMeasuredHeight());
                break;
            case R.id.map_layout:
                showToast("地图");
                break;
        }
    }

    private void setTabSelected(Button btnSelected, LinearLayout layout) {
        Drawable selectedDrawable = getResources().getDrawable(R.drawable.shape_nav_indicator);
        int right = 200;
        selectedDrawable.setBounds(0, 0, right, 4);
        btnSelected.setSelected(true);
        btnSelected.setCompoundDrawables(null, null, null, selectedDrawable);
        int size = layout.getChildCount();
        for (int i = 0; i < size; i++) {
            if (btnSelected.getId() != layout.getChildAt(i).getId()) {
                layout.getChildAt(i).setSelected(false);
                ((Button) layout.getChildAt(i)).setCompoundDrawables(null, null, null, null);
            }
        }
    }
}
